# -*- coding: utf-8 -*-

import Header
import logging
from TouTiaoClass import TouTiaoClass
from IQiYiClass import IQiYiClass
from QQClass import QQClass


import pymysql
import pymysql.cursors
import threading
from time import ctime,sleep


import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#上面三行用来保证把中文存储到数据库中


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='CCMain.log',
                    filemode='w')

def toutiaoHandle(uId, videoId):
    print('toutiaoHandle uId = %s, videoId = %s' % (uId, videoId))
    logging.debug('toutiao Doing')
    tt = TouTiaoClass(uId, videoId)
    tt.distribute()

def iqiyiHandle(uId, videoId):
    print('iqiyiHandle uId = %s, videoId = %s' % (uId, videoId))
    logging.debug('iqiyi Doing')
    iqiyi = IQiYiClass(uId, videoId)
    iqiyi.distribute()

def QQHandle(uId, videoId):
    print('QQHandle uId = %s, videoId = %s' % (uId, videoId))
    logging.debug('QQHandle Doing')

def bilibiliHandle(uId, videoId):
    print('bilibiliHandle uId = %s, videoId = %s' % (uId, videoId))
    logging.debug('bilibiliHandle Doing')

def main():

    for i in range(1, len(sys.argv)):
        logging.debug('值为%s', sys.argv[i])

    uId = sys.argv[1]
    videoId = sys.argv[2]
    logging.debug('uId = %s',uId)
    logging.debug('videoId = %s',videoId)

    return

    #从数据库表中读取任务
    dbConnect = pymysql.connect('127.0.0.1', 'root', 'KeYpZrZx', 'chuanchuan', charset='utf8')
    dbCursor = dbConnect.cursor(cursor=pymysql.cursors.DictCursor)

    sql = 'SELECT platform FROM `tv_release` WHERE uid = %s AND video_id = %s'
    dbCursor.execute(sql, (uId, videoId))
    result = dbCursor.fetchall()

    arrayPlatform = [] #保存到数组中
    for item in result:
        print item['platform']
        logging.debug('item platform = %s', item['platform'])
        arrayPlatform.append(item['platform'])

    print arrayPlatform
    logging.debug('arrayPlatform = %s', arrayPlatform)
    dbCursor.close()
    dbConnect.close()

    print "start doing %s" % ctime()
    logging.debug('start doing %s', ctime())

    threads = [] #线程数组, 并行处理
    for channel in arrayPlatform:
        if channel == 1:
            print '头条号'
            thd = threading.Thread(target=toutiaoHandle, args=(uId,videoId))
            threads.append(thd)
        elif channel == 2:
            print '哔哩哔哩'
            thd = threading.Thread(target=bilibiliHandle, args=(uId, videoId))
            threads.append(thd)
        elif channel == 3:
            print '爱奇艺'
            thd = threading.Thread(target=iqiyiHandle, args=(uId, videoId))
            threads.append(thd)
        elif channel == 4:
            print '企鹅号'
            thd = threading.Thread(target=QQHandle, args=(uId, videoId))
            threads.append(thd)

    for t in threads:
        t.setDaemon(True)
        t.start()
    t.join()

    print "all over %s" % ctime()
    logging.debug('all over %s', ctime())

if __name__ == "__main__":
    main()